/*
 Copyright 2009 Christian Sprecher

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

class PropertyViewerController {
    // these will be injected by Griffon
    def model
    def view
    def builder
    def helper

    void mvcGroupInit(Map args) {
        model.property = args.property
        helper=args.helper
        model.mode= args.mode?:model.DISPLAY_MODE
        // this method is called after model and view are injected
    }

    def toggleMode = {
        view.valuePanel.removeAll()
        if(model.mode==model.VIEW_MODE) {
            view.saveButton.enabled = true
            view.modeButton.text="Cancel"
            model.mode=model.EDIT_MODE
            view.valuePanel.add(view.createEditor(model.property))
        } else {
            view.saveButton.enabled = false
            view.modeButton.text="Edit"
            model.mode=model.VIEW_MODE
            view.valuePanel.add(view.createViewer(model.property))
        }
        view.valuePanel.validate()
        view.valuePanel.repaint()
    }

    def saveProperty = {
        helper.saveProperty(model.property.path, view.retrieveValues())
        toggleMode()
    }

}